home *** CD-ROM | disk | FTP | other *** search
- //
- // BEGIN FLOCK GPL
- //
- // Copyright Flock Inc. 2005-2007
- // http://flock.com
- //
- // This file may be used under the terms of of the
- // GNU General Public License Version 2 or later (the "GPL"),
- // http://www.gnu.org/licenses/gpl.html
- //
- // Software distributed under the License is distributed on an "AS IS" basis,
- // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- // for the specific language governing rights and limitations under the
- // License.
- //
- // END FLOCK GPL
- //
-
- const CLASS_ID = Components.ID("{b2045e5e-c742-4dbc-8fae-233f428bc9c0}");
- const CLASS_NAME = "Flock Logging File Writer";
- const CONTRACT_ID = "@flock.com/logging-file-writer;1";
-
- const LOGGER_FILENAME = "log.txt"
-
- function flockLoggingFileWriter()
- {
- this._initialized = false;
-
- var obs = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
- obs.addObserver(this, "profile-after-change", false);
- }
-
- flockLoggingFileWriter.prototype = {
-
- _initialized: null,
-
- _initFile: function()
- {
- // Prepare the log file for the requested module
- var dirService = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);
- var profileDir = dirService.get('ProfD', Components.interfaces.nsILocalFile);
- var file = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
- file.initWithPath(profileDir.path);
- file.append(LOGGER_FILENAME);
- if(!file.exists()) file.createUnique(0,0600);
-
- var transport = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
- transport.init(file, 0x04 | 0x08 | 0x10, 064, 0);
-
- this._converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
- this._converter.charset = "UTF-8";
-
- this._outputStream = Components.classes["@mozilla.org/network/buffered-output-stream;1"].createInstance(Components.interfaces.nsIBufferedOutputStream);
- this._outputStream.init(transport, 65536 * 4);
-
- this._initialized = true;
- },
-
- emit: function(aDate, aLevel, aContext, aMessage)
- {
- if (this._initialized) {
- var levels = new Array("all","debug", "info", "warn", "error", "fatal");
-
- var date = new Date(aDate);
- var dateString = date.getFullYear() + "-" + this._pad(date.getMonth() + 1, 2) + "-" + this._pad(date.getDate(), 2) + " " + this._pad(date.getHours(), 2) + ":" + this._pad(date.getMinutes(), 2) + ":" + this._pad(date.getSeconds(), 2) + "." + this._pad(date.getTime() % 1000, 3);
- var content = "[" + dateString + " " + aContext + ":" + levels[aLevel] + "] " + aMessage + "\n";
-
- var inputStream = this._converter.convertToInputStream(content);
-
- this._outputStream.writeFrom(inputStream, inputStream.available());
- this._outputStream.flush();
- }
- },
-
- _pad: function(aNumber, aPlaces)
- {
- var numberString = aNumber + "";
- while (numberString.length < aPlaces) {
- numberString = "0" + numberString;
- }
- return numberString;
- },
-
- // nsIObserver
- observe: function(subject, topic, state)
- {
- switch (topic) {
- case "profile-after-change":
- // init log file
- this._initFile();
- break;
- }
- },
-
- // nsIClassInfo
- getInterfaces: function(aCount)
- {
- var interfaces = [Components.interfaces.flockILoggingObserver, Components.interfaces.nsIClassInfo];
- aCount.value = interfaces.length;
- return interfaces;
- },
-
- // nsIClassInfo
- getHelperForLanguage: function(aLanguage)
- {
- return null;
- },
-
- // nsIClassInfo
- contractID: CONTRACT_ID,
-
- // nsIClassInfo
- classDescription: CLASS_NAME,
-
- // nsIClassInfo
- classID: CLASS_ID,
-
- // nsIClassInfo
- implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
-
- // nsIClassInfo
- flags: Components.interfaces.nsIClassInfo.SINGLETON,
-
- // nsISupports
- QueryInterface: function(aIID)
- {
- if (!aIID.equals(Components.interfaces.nsISupports) && !aIID.equals(Components.interfaces.flockILoggingObserver) && !aIID.equals(Components.interfaces.nsIObserver) && !aIID.equals(Components.interfaces.nsIClassInfo))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- return this;
- }
-
- };
-
- /******************************************************************************
- * XPCOM Functions for construction and registration
- ******************************************************************************/
- var Module = {
- _firstTime: true,
- registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
- {
- if (this._firstTime) {
- this._firstTime = false;
- throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
- }
- aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
- aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
-
- // Make the Logging Observer a default
- var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
- .getService(Components.interfaces.nsICategoryManager);
- categoryManager.addCategoryEntry("flockILoggingObserver", CLASS_NAME, CONTRACT_ID, true, true, null);
- },
-
- unregisterSelf: function(aCompMgr, aLocation, aType)
- {
- aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
- aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);
- },
-
- getClassObject: function(aCompMgr, aCID, aIID)
- {
- if (!aIID.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
- if (aCID.equals(CLASS_ID))
- return Factory;
- throw Components.results.NS_ERROR_NO_INTERFACE;
- },
-
- canUnload: function(aCompMgr) { return true; }
- };
-
- var Factory = {
- createInstance: function(aOuter, aIID)
- {
- if (aOuter != null)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
- return (new flockLoggingFileWriter()).QueryInterface(aIID);
- }
- };
-
- function NSGetModule(aCompMgr, aFileSpec) { return Module; }
-
-